package com.zlf.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlf.blog.dao.pojo.SysUser;
import com.zlf.blog.service.SysUserService;
import com.zlf.blog.dao.mapper.SysUserMapper;
import com.zlf.blog.utils.JWTUtils;
import com.zlf.blog.utils.UserThreadLocal;
import com.zlf.blog.vo.ChangeUserInfoVo;
import com.zlf.blog.vo.ErrorCode;
import com.zlf.blog.vo.LoginUserVo;
import com.zlf.blog.vo.Result;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* @author zlfi1
* @description 针对表【ms_sys_user】的数据库操作Service实现
* @createDate 2022-07-02 09:33:15
*/
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
implements SysUserService {
    private static final String slat = "Luffy!@#";
    private final SysUserMapper sysUserMapper;
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public SysUser findSysUserById(String id) {

        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setNickname("Luffy");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String pwd) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.eq(SysUser::getPassword, pwd);
        queryWrapper.select(SysUser::getId, SysUser::getAccount, SysUser::getAvatar, SysUser::getNickname);
        queryWrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    @Override
    public Result getUserInfoByToken(String token) {
        {
            Map<String, Object> map = JWTUtils.checkToken(token);
            if (map == null) {
                return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
            }
            String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
            if (StringUtils.isBlank(userJson)) {
                return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
            }
            SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
            LoginUserVo loginUserVo = new LoginUserVo();
            loginUserVo.setAvatar(sysUser.getAvatar());
            loginUserVo.setId(sysUser.getId());
            loginUserVo.setNickname(sysUser.getNickname());
            return Result.success(loginUserVo);
        }
    }
    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean save(SysUser sysUser) {
        //注意 默认生成的id 是分布式id 采用了雪花算法
        this.sysUserMapper.insert(sysUser);
        return true;
    }

    @Override
    public int changeInfo(ChangeUserInfoVo changeUserInfoVo,String id) {
        changeUserInfoVo.setPassWord(DigestUtils.md5Hex(changeUserInfoVo.getPassWord()+slat));
        return sysUserMapper.updateUserInfo(changeUserInfoVo,id);

    }
}
